package com.source.diyjuc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author wengqy
 * @Date 2021/1/18
 */
public class ABADemo {
    static AtomicReference<Integer> atomicReference = new AtomicReference<>(100);

    public static void main(String[] args) {

        new Thread(()->{ atomicReference.compareAndSet(100,101);

        atomicReference.compareAndSet(101,100); },"T1").start();

        new Thread(()->{
            // 暂停一秒钟，保证上面线程先执行
           try {
               TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
           System.out.println(atomicReference.compareAndSet(100, 2019));
           // 修改成功！
            System.out.println(atomicReference.get()); },"T2").start();
    }
}
